package com.punch.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.exception.BusinessException;
import com.punch.common.page.PageInfo;
import com.punch.common.query.order.SettlementQueryInfo;
import com.punch.common.tools.BasePage;
import com.punch.common.vo.order.ClassSettlementPageVO;
import com.punch.common.vo.order.SettlementPageVO;
import com.punch.order.exception.OrderExceptionEnum;
import com.punch.order.mapper.CourseSettlementDetailMapper;
import com.punch.order.mapper.CourseSettlementMapper;
import com.punch.order.model.CourseSettlement;
import com.punch.order.service.CourseSettlementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 课程结算表 服务实现类
 * </p>
 *
 * @author wjj
 * @since 2020-09-26
 */
@Service
public class CourseSettlementServiceImpl extends ServiceImpl<CourseSettlementMapper, CourseSettlement> implements CourseSettlementService {

    @Resource
    private CourseSettlementDetailMapper courseSettlementDetailMapper;

    /**
     * 课程结算列表
     *
     * @param settlementQueryInfo 结算订单查询信息
     * @return SettlementPageVO
     */
    @Override
    public PageInfo<SettlementPageVO> listCourseSettlement(SettlementQueryInfo settlementQueryInfo) {
        return BasePage.queryBasePage(settlementQueryInfo, params -> this.getBaseMapper().queryCount(params),
                params -> this.getBaseMapper().queryList(params));
    }

    /**
     * 课程结算班级列表
     *
     * @param settlementQueryInfo 结算订单查询信息
     * @return ClassSettlementPageVO
     */
    @Override
    public PageInfo<ClassSettlementPageVO> listCourseClassSettlement(SettlementQueryInfo settlementQueryInfo) {

        return BasePage.queryBasePage(settlementQueryInfo, params -> courseSettlementDetailMapper.queryCount(params),
                params -> courseSettlementDetailMapper.queryList(params));
    }

    /**
     * 教具结算修改状态
     *
     * @param settlementId     结算id
     * @param settlementStatus
     * @return settlementId
     */
    @Override
    public Long updateCourseSettlementStatus(Long settlementId, Integer settlementStatus) {
        CourseSettlement courseSettlementEX = getById(settlementId);
        if (courseSettlementEX == null) {
            throw new BusinessException(OrderExceptionEnum.COURSE_SETTLEMENT_NOT_EXIST);
        }
        Integer currentStatus = courseSettlementEX.getSettlementStatus();
        //待确认-->已确认
        if (settlementStatus == 1) {
            //判断状态是否是待确认
            if (currentStatus != 0) {
                throw new BusinessException(OrderExceptionEnum.COURSE_SETTLEMENT_STATUS_ERROR);
            }
        }
        //已确认-->已打款
        if (settlementStatus == 2) {
            //判断状态是否是待确认
            if (currentStatus != 1) {
                throw new BusinessException(OrderExceptionEnum.MATERIAL_SETTLEMENT_STATUS_ERROR);
            }
        }
        CourseSettlement courseSettlement = new CourseSettlement();
        courseSettlement.setId(settlementId);
        courseSettlement.setSettlementStatus(settlementStatus);
        updateById(courseSettlement);
        return settlementId;
    }
}
